home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / ANSI / c-client / dummy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  13.5 KB  |  555 lines

  1. /*
  2.  * Program:    Dummy routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    9 May 1991
  13.  * Last Edited:    15 July 1993
  14.  *
  15.  * Copyright 1993 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. extern int errno;        /* just in case */
  41. #include "mail.h"
  42. #include "osdep.h"
  43. #include <pwd.h>
  44. #include <sys/file.h>
  45. #include "dummy.h"
  46. #include "misc.h"
  47.  
  48. /* Dummy routines */
  49.  
  50.  
  51. /* Driver dispatch used by MAIL */
  52.  
  53. DRIVER dummydriver = {
  54.   "dummy",            /* driver name */
  55.   (DRIVER *) NIL,        /* next driver */
  56.   dummy_valid,            /* mailbox is valid for us */
  57.   dummy_parameters,        /* manipulate parameters */
  58.   dummy_find,            /* find mailboxes */
  59.   dummy_find_bboards,        /* find bboards */
  60.   dummy_find_all,        /* find all mailboxes */
  61.   dummy_find_all_bboards,    /* find all bboards */
  62.   dummy_subscribe,        /* subscribe to mailbox */
  63.   dummy_unsubscribe,        /* unsubscribe from mailbox */
  64.   dummy_subscribe_bboard,    /* subscribe to bboard */
  65.   dummy_unsubscribe_bboard,    /* unsubscribe from bboard */
  66.   dummy_create,            /* create mailbox */
  67.   dummy_delete,            /* delete mailbox */
  68.   dummy_rename,            /* rename mailbox */
  69.   dummy_open,            /* open mailbox */
  70.   dummy_close,            /* close mailbox */
  71.   dummy_fetchfast,        /* fetch message "fast" attributes */
  72.   dummy_fetchflags,        /* fetch message flags */
  73.   dummy_fetchstructure,        /* fetch message structure */
  74.   dummy_fetchheader,        /* fetch message header only */
  75.   dummy_fetchtext,        /* fetch message body only */
  76.   dummy_fetchbody,        /* fetch message body section */
  77.   dummy_setflag,        /* set message flag */
  78.   dummy_clearflag,        /* clear message flag */
  79.   dummy_search,            /* search for message based on criteria */
  80.   dummy_ping,            /* ping mailbox to see if still alive */
  81.   dummy_check,            /* check for new messages */
  82.   dummy_expunge,        /* expunge deleted messages */
  83.   dummy_copy,            /* copy messages to another mailbox */
  84.   dummy_move,            /* move messages to another mailbox */
  85.   dummy_append,            /* append string message to mailbox */
  86.   dummy_gc            /* garbage collect stream */
  87. };
  88.  
  89. /* Dummy validate mailbox
  90.  * Accepts: mailbox name
  91.  * Returns: our driver if name is valid, NIL otherwise
  92.  */
  93.  
  94. DRIVER *dummy_valid (char *name)
  95. {
  96.   return (name && *name) ? &dummydriver : NIL;
  97. }
  98.  
  99.  
  100. /* Dummy manipulate driver parameters
  101.  * Accepts: function code
  102.  *        function-dependent value
  103.  * Returns: function-dependent return value
  104.  */
  105.  
  106. void *dummy_parameters (long function,void *value)
  107. {
  108.   return NIL;
  109. }
  110.  
  111. /* Dummy find list of subscribed mailboxes
  112.  * Accepts: mail stream
  113.  *        pattern to search
  114.  */
  115.  
  116. void dummy_find (MAILSTREAM *stream,char *pat)
  117. {
  118.   void *sdb = NIL;
  119.   DIR *dirp;
  120.   struct direct *d;
  121.   char tmp[MAILTMPLEN],file[MAILTMPLEN];
  122.   char *s,*t;
  123.   int i = 0;
  124.                 /* no-op if have a subscription database */
  125.   if (sm_read (&sdb)) fs_give ((void **) &sdb);
  126.   else {            /* no subscriptions, do a directory */
  127.     if (s = strrchr (pat,'/')) {/* directory specified in pattern? */
  128.       strncpy (file,pat,i = (++s) - pat);
  129.       file[i] = '\0';        /* tie off prefix */
  130.       t = dummy_file (tmp,pat);    /* make fully-qualified file name */
  131.                 /* tie off directory name */
  132.       if (s = strrchr (t,'/')) *s = '\0';
  133.     }
  134.     else t = myhomedir ();    /* use home directory to search */
  135.     if (dirp = opendir (t)) {    /* now open that directory */
  136.       while (d = readdir (dirp)) {/* for each directory entry */
  137.     strcpy (file + i,d->d_name);
  138.     if (pmatch (file,pat)) mm_mailbox (file);
  139.       }
  140.       closedir (dirp);        /* flush directory */
  141.     }
  142.   }
  143. }
  144.  
  145. /* Dummy find list of subscribed bboards
  146.  * Accepts: mail stream
  147.  *        pattern to search
  148.  */
  149.  
  150. void dummy_find_bboards (MAILSTREAM *stream,char *pat)
  151. {
  152.   void *sdb = NIL;
  153.   DIR *dirp;
  154.   struct direct *d;
  155.   struct passwd *pw;
  156.   char tmp[MAILTMPLEN],file[MAILTMPLEN];
  157.   int i = 1;
  158.   char *s;
  159.                 /* no-op if have a subscription database */
  160.   if (sm_read (&sdb)) fs_give ((void **) &sdb);
  161.   else {
  162.     if (!((pw = getpwnam ("ftp")) && pw->pw_dir)) return;
  163.     file[0] = '*';        /* bboard designator */
  164.                 /* directory specified in pattern? */
  165.     if (s = strrchr (pat,'/')) strncpy (file + 1,pat,i += (++s) - pat);
  166.     file[i] = '\0';        /* tie off prefix */
  167.     sprintf (tmp,"%s/%s",pw->pw_dir,(file[1] == '/') ? file + 2 : file + 1);
  168.     if (dirp = opendir (tmp)) {    /* now open that directory */
  169.       while (d = readdir (dirp)) {/* for each directory entry */
  170.     strcpy (file + i,d->d_name);
  171.     if (pmatch (file + 1,pat)) mm_bboard (file + 1);
  172.       }
  173.       closedir (dirp);        /* flush directory */
  174.     }
  175.   }
  176. }
  177.  
  178. /* Dummy find list of all mailboxes
  179.  * Accepts: mail stream
  180.  *        pattern to search
  181.  */
  182.  
  183. void dummy_find_all (MAILSTREAM *stream,char *pat)
  184. {
  185.                 /* always an INBOX */
  186.   if (pmatch ("INBOX",pat)) mm_mailbox ("INBOX");
  187. }
  188.  
  189.  
  190. /* Dummy find list of all bboards
  191.  * Accepts: mail stream
  192.  *        pattern to search
  193.  */
  194.  
  195. void dummy_find_all_bboards (MAILSTREAM *stream,char *pat)
  196. {
  197.   /* Exit quietly */
  198. }
  199.  
  200. /* Dummy subscribe to mailbox
  201.  * Accepts: mail stream
  202.  *        mailbox to add to subscription list
  203.  * Returns: T on success, NIL on failure
  204.  */
  205.  
  206. long dummy_subscribe (MAILSTREAM *stream,char *mailbox)
  207. {
  208.   return NIL;            /* always fails */
  209. }
  210.  
  211.  
  212. /* Dummy unsubscribe to mailbox
  213.  * Accepts: mail stream
  214.  *        mailbox to delete from subscription list
  215.  * Returns: T on success, NIL on failure
  216.  */
  217.  
  218. long dummy_unsubscribe (MAILSTREAM *stream,char *mailbox)
  219. {
  220.   return NIL;            /* always fails */
  221. }
  222.  
  223.  
  224. /* Dummy subscribe to bboard
  225.  * Accepts: mail stream
  226.  *        bboard to add to subscription list
  227.  * Returns: T on success, NIL on failure
  228.  */
  229.  
  230. long dummy_subscribe_bboard (MAILSTREAM *stream,char *mailbox)
  231. {
  232.   return NIL;            /* always fails */
  233. }
  234.  
  235.  
  236. /* Dummy unsubscribe to bboard
  237.  * Accepts: mail stream
  238.  *        bboard to delete from subscription list
  239.  * Returns: T on success, NIL on failure
  240.  */
  241.  
  242. long dummy_unsubscribe_bboard (MAILSTREAM *stream,char *mailbox)
  243. {
  244.   return NIL;            /* always fails */
  245. }
  246.  
  247. /* Dummy create mailbox
  248.  * Accepts: mail stream
  249.  *        mailbox name to create
  250.  *        driver type to use
  251.  * Returns: T on success, NIL on failure
  252.  */
  253.  
  254. long dummy_create (MAILSTREAM *stream,char *mailbox)
  255. {
  256.   return NIL;            /* always fails */
  257. }
  258.  
  259.  
  260. /* Dummy delete mailbox
  261.  * Accepts: mail stream
  262.  *        mailbox name to delete
  263.  * Returns: T on success, NIL on failure
  264.  */
  265.  
  266. long dummy_delete (MAILSTREAM *stream,char *mailbox)
  267. {
  268.   return NIL;            /* always fails */
  269. }
  270.  
  271.  
  272. /* Mail rename mailbox
  273.  * Accepts: mail stream
  274.  *        old mailbox name
  275.  *        new mailbox name
  276.  * Returns: T on success, NIL on failure
  277.  */
  278.  
  279. long dummy_rename (MAILSTREAM *stream,char *old,char *new)
  280. {
  281.   return NIL;            /* always fails */
  282. }
  283.  
  284. /* Dummy open
  285.  * Accepts: stream to open
  286.  * Returns: stream on success, NIL on failure
  287.  */
  288.  
  289. MAILSTREAM *dummy_open (MAILSTREAM *stream)
  290. {
  291.   int fd;
  292.   char tmp[MAILTMPLEN];
  293.                 /* OP_PROTOTYPE call or silence */
  294.   if (!stream || stream->silent) return NIL;
  295.   if (*stream->mailbox == '*')    /* is it a bboard? */
  296.     sprintf (tmp,"No such bboard: %s",stream->mailbox+1);
  297.                 /* remote specification? */
  298.   else if (*stream->mailbox == '{')
  299.     sprintf (tmp,"Invalid remote specification: %s",stream->mailbox);
  300.   else if ((fd = open (dummy_file (tmp,stream->mailbox),O_RDONLY,NIL)) < 0)
  301.     sprintf (tmp,"%s: %s",strerror (errno),stream->mailbox);
  302.   else {            /* must be bogus format file */
  303.     sprintf (tmp,"%s is not a mailbox",stream->mailbox);
  304.     close (fd);            /* toss out the fd */
  305.   }
  306.   mm_log (tmp,ERROR);
  307.   return NIL;            /* always fails */
  308. }
  309.  
  310.  
  311. /* Dummy close
  312.  * Accepts: MAIL stream
  313.  */
  314.  
  315. void dummy_close (MAILSTREAM *stream)
  316. {
  317.   /* Exit quietly */
  318. }
  319.  
  320. /* Dummy fetch fast information
  321.  * Accepts: MAIL stream
  322.  *        sequence
  323.  */
  324.  
  325. void dummy_fetchfast (MAILSTREAM *stream,char *sequence)
  326. {
  327.   fatal ("Impossible dummy_fetchfast");
  328. }
  329.  
  330.  
  331. /* Dummy fetch flags
  332.  * Accepts: MAIL stream
  333.  *        sequence
  334.  */
  335.  
  336. void dummy_fetchflags (MAILSTREAM *stream,char *sequence)
  337. {
  338.   fatal ("Impossible dummy_fetchflags");
  339. }
  340.  
  341.  
  342. /* Dummy fetch envelope
  343.  * Accepts: MAIL stream
  344.  *        message # to fetch
  345.  *        pointer to return body
  346.  * Returns: envelope of this message, body returned in body value
  347.  */
  348.  
  349. ENVELOPE *dummy_fetchstructure (MAILSTREAM *stream,long msgno,BODY **body)
  350. {
  351.   fatal ("Impossible dummy_fetchstructure");
  352.   return NIL;
  353. }
  354.  
  355.  
  356. /* Dummy fetch message header
  357.  * Accepts: MAIL stream
  358.  *        message # to fetch
  359.  * Returns: message header in RFC822 format
  360.  */
  361.  
  362. char *dummy_fetchheader (MAILSTREAM *stream,long msgno)
  363. {
  364.   fatal ("Impossible dummy_fetchheader");
  365.   return NIL;
  366. }
  367.  
  368. /* Dummy fetch message text (body only)
  369.  * Accepts: MAIL stream
  370.  *        message # to fetch
  371.  * Returns: message text in RFC822 format
  372.  */
  373.  
  374. char *dummy_fetchtext (MAILSTREAM *stream,long msgno)
  375. {
  376.   fatal ("Impossible dummy_fetchtext");
  377.   return NIL;
  378. }
  379.  
  380.  
  381. /* Berkeley fetch message body as a structure
  382.  * Accepts: Mail stream
  383.  *        message # to fetch
  384.  *        section specifier
  385.  * Returns: pointer to section of message body
  386.  */
  387.  
  388. char *dummy_fetchbody (MAILSTREAM *stream,long m,char *sec,unsigned long *len)
  389. {
  390.   fatal ("Impossible dummy_fetchbody");
  391.   return NIL;
  392. }
  393.  
  394.  
  395. /* Dummy set flag
  396.  * Accepts: MAIL stream
  397.  *        sequence
  398.  *        flag(s)
  399.  */
  400.  
  401. void dummy_setflag (MAILSTREAM *stream,char *sequence,char *flag)
  402. {
  403.   fatal ("Impossible dummy_setflag");
  404. }
  405.  
  406.  
  407. /* Dummy clear flag
  408.  * Accepts: MAIL stream
  409.  *        sequence
  410.  *        flag(s)
  411.  */
  412.  
  413. void dummy_clearflag (MAILSTREAM *stream,char *sequence,char *flag)
  414. {
  415.   fatal ("Impossible dummy_clearflag");
  416. }
  417.  
  418.  
  419. /* Dummy search for messages
  420.  * Accepts: MAIL stream
  421.  *        search criteria
  422.  */
  423.  
  424. void dummy_search (MAILSTREAM *stream,char *criteria)
  425. {
  426.   fatal ("Impossible dummy_search");
  427. }
  428.  
  429. /* Dummy ping mailbox
  430.  * Accepts: MAIL stream
  431.  * Returns: T if stream alive, else NIL
  432.  * No-op for readonly files, since read/writer can expunge it from under us!
  433.  */
  434.  
  435. long dummy_ping (MAILSTREAM *stream)
  436. {
  437.   fatal ("Impossible dummy_ping");
  438.   return NIL;
  439. }
  440.  
  441.  
  442. /* Dummy check mailbox
  443.  * Accepts: MAIL stream
  444.  * No-op for readonly files, since read/writer can expunge it from under us!
  445.  */
  446.  
  447. void dummy_check (MAILSTREAM *stream)
  448. {
  449.   fatal ("Impossible dummy_check");
  450. }
  451.  
  452.  
  453. /* Dummy expunge mailbox
  454.  * Accepts: MAIL stream
  455.  */
  456.  
  457. void dummy_expunge (MAILSTREAM *stream)
  458. {
  459.   fatal ("Impossible dummy_expunge");
  460. }
  461.  
  462. /* Dummy copy message(s)
  463.  * Accepts: MAIL stream
  464.  *        sequence
  465.  *        destination mailbox
  466.  * Returns: T if copy successful, else NIL
  467.  */
  468.  
  469. long dummy_copy (MAILSTREAM *stream,char *sequence,char *mailbox)
  470. {
  471.   fatal ("Impossible dummy_copy");
  472.   return NIL;
  473. }
  474.  
  475.  
  476. /* Dummy move message(s)
  477.  * Accepts: MAIL stream
  478.  *        sequence
  479.  *        destination mailbox
  480.  * Returns: T if move successful, else NIL
  481.  */
  482.  
  483. long dummy_move (MAILSTREAM *stream,char *sequence,char *mailbox)
  484. {
  485.   fatal ("Impossible dummy_move");
  486.   return NIL;
  487. }
  488.  
  489.  
  490. /* Dummy append message string
  491.  * Accepts: mail stream
  492.  *        destination mailbox
  493.  *        stringstruct of message to append
  494.  * Returns: T on success, NIL on failure
  495.  */
  496.  
  497. long dummy_append (MAILSTREAM *stream,char *mailbox,STRING *message)
  498. {
  499.   int fd = -1,e;
  500.   char tmp[MAILTMPLEN];
  501.                 /* see if such a file */
  502.   if ((*mailbox != '*') && (*mailbox != '{') &&
  503.       (fd = open (dummy_file (tmp,mailbox),O_RDONLY,NIL)) < 0) {
  504.     if ((e = errno) == ENOENT)    /* failed, was it no such file? */
  505.       mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
  506.     sprintf (tmp,"%s: %s",strerror (e),mailbox);
  507.   }
  508.   else {            /* must be bogus format file */
  509.     sprintf (tmp,"%s is not a valid mailbox",mailbox);
  510.     close (fd);            /* toss out the fd */
  511.   }
  512.   mm_log (tmp,ERROR);        /* pass up error */
  513.   return NIL;            /* always fails */
  514. }
  515.  
  516.  
  517. /* Dummy garbage collect stream
  518.  * Accepts: mail stream
  519.  *        garbage collection flags
  520.  */
  521.  
  522. void dummy_gc (MAILSTREAM *stream,long gcflags)
  523. {
  524.   fatal ("Impossible dummy_gc");
  525. }
  526.  
  527. /* Dummy mail generate file string
  528.  * Accepts: temporary buffer to write into
  529.  *        mailbox name string
  530.  * Returns: local file string
  531.  */
  532.  
  533. char *dummy_file (char *dst,char *name)
  534. {
  535.   struct passwd *pw;
  536.   char *s,*t,tmp[MAILTMPLEN];
  537.   switch (*name) {
  538.   case '/':            /* absolute file path */
  539.     strcpy (dst,name);        /* copy the mailbox name */
  540.     break;
  541.   case '~':            /* home directory */
  542.     if (name[1] == '/') t = myhomedir ();
  543.     else {
  544.       strcpy (tmp,name + 1);    /* copy user name */
  545.       if (s = strchr (tmp,'/')) *s = '\0';
  546.       t = ((pw = getpwnam (tmp)) && pw->pw_dir) ? pw->pw_dir : "/NOSUCHUSER";
  547.     }
  548.     sprintf (dst,"%s%s",t,(s = strchr (name,'/')) ? s : "");
  549.     break;
  550.   default:            /* any other name */
  551.     sprintf (dst,"%s/%s",myhomedir (),name);
  552.   }
  553.   return dst;
  554. }
  555.